package com.viewhigh.wechat.sdk.util;

import com.viewhigh.framework.common.log.LogUtils;
import com.viewhigh.wechat.sdk.aes.AesException;
import com.viewhigh.wechat.sdk.aes.WXBizMsgCrypt;

/**
 * @Title: CryptMsgUtil.java
 * @Package com.viewhigh.healthwx.util
 * @Description: 微信加解密工具
 * @author liusongqing
 * @Company www.viewhigh.com
 * @date 2016年7月19日 下午5:24:25
 * @version V1.0
 */
public class CryptMsgUtil {
    /**
     * 
     * @Title: decryptMsg @Description: 解密微信发过来的密文 @param @param msgSignature @param @param timeStamp @param @param
     *         nonce @param @param encrypt_msg @param @return 设定文件 @return String 返回类型 @throws
     */
    public static String decryptMsg(String msgSignature, String timeStamp, String nonce, String encrypt_msg,
            String token, String appId, String encodingAesKey) {
        WXBizMsgCrypt pc;
        String result = "";
        try {
            pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
            result = pc.decryptMsg(msgSignature, timeStamp, nonce, encrypt_msg);
        } catch (AesException e) {
            LogUtils.error("CryptMsgUtil decryptMsg 解密微信发过来的密文失败", e);
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 
     * @Title: ecryptMsg @Description: 加密给微信的消息内容 @param @param replayMsg @param @param timeStamp @param @param
     *         nonce @param @return 设定文件 @return String 返回类型 @throws
     */
    public static String ecryptMsg(String replayMsg, String timeStamp, String nonce, String token, String appId,
            String encodingAesKey) {
        WXBizMsgCrypt pc;
        String result = "";
        try {
            pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
            result = pc.encryptMsg(replayMsg, timeStamp, nonce);
        } catch (AesException e) {
            LogUtils.error("CryptMsgUtil ecryptMsg 加密给微信的消息内容失败", e);
            e.printStackTrace();
        }
        return result;
    }
}
